package layers;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class LayerOneServer {
	
	DatagramSocket serverSocket;
    DatagramPacket receivePacket;
    DatagramPacket sendPacket;
	
	public LayerOneServer() throws SocketException{
		serverSocket = new DatagramSocket(9876);
	}

	public Object[] serverReceived() throws IOException{
 		byte[] receiveData = new byte[1024];
		Object data[] = new Object[3];
		receivePacket = new DatagramPacket(receiveData, receiveData.length);
		serverSocket.receive(receivePacket);
		data[0] = new String(receivePacket.getData());
		data[1] = receivePacket.getAddress();
		data[2] = receivePacket.getPort();
        return data;
       }
	
	public void serverSend(String sentence, String host, int port) throws IOException{
	    byte[] sendData = new byte[1024];
        String ack = sentence.toUpperCase();
        sendData = ack.getBytes();
        InetAddress IPAddress = InetAddress.getByName(host);
        int portClient = port;
        sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, portClient);
        serverSocket.send(sendPacket);
	}
	
}
